package com.example.librarymanagement.controller;


import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.librarymanagement.dto.book.BookAddDto;
import com.example.librarymanagement.dto.book.BookListDto;
import com.example.librarymanagement.dto.book.BookUpdDto;
import com.example.librarymanagement.entity.Book;
import com.example.librarymanagement.entity.BookAddRecord;
import com.example.librarymanagement.result.ResultCode;
import com.example.librarymanagement.util.CheckEntityUtil;
import com.example.librarymanagement.util.EnumConst;
import com.example.librarymanagement.vo.BookVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author zjia
 */
@RestController
@Api(tags = "图书模块")
@RequestMapping("/book")
public class BookController extends BaseController {

    @GetMapping
    @ApiOperation(value = "查询书籍列表")
    @PreAuthorize("hasAnyAuthority('book:manage:list','book:list:view')")
    public IPage<BookVo> getBook(@Validated BookListDto bookListDto) {
        return bookService.getBookList(bookListDto);
    }

    @GetMapping("/info/{id}")
    @ApiOperation(value = "查询书籍信息")
    @PreAuthorize("hasAnyAuthority('book:manage:list','book:list:view')")
    public Book getBook(@PathVariable Integer id) {
        return bookService.getById(id);
    }

    @PostMapping
    @Transactional(rollbackFor = {Exception.class})
    @ApiOperation(value = "增加书本信息")
    @PreAuthorize("hasAuthority('book:manage:save')")
    public void addBook(@Validated @RequestBody BookAddDto bookAddDto) {

        CheckEntityUtil.isNotNullByColumnEq("isbn", bookAddDto.getIsbn(), bookService, ResultCode.BOOK_ACCOUNT_EXIST);

        CheckEntityUtil.isNullById(bookAddDto.getTypeId(), typeService, ResultCode.BOOK_TYPE_NOT_EXIST);

        Book book = new Book();
        bookAddDto.convertToEntity(book);
        book.setCount(book.getTotal());
        bookService.save(book);
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>(book);
        Book one = bookService.getOne(queryWrapper);
        BookAddRecord bookAddRecord = new BookAddRecord();
        bookAddRecord.setBookId(one.getId());
        bookAddRecord.setAddTime(new Date());
        bookAddRecord.setNumber(bookAddDto.getTotal());
        bookAddRecord.setBookPeriodical(EnumConst.BOOK.getCode());
        bookAddRecordService.save(bookAddRecord);
    }

    @PutMapping
    @ApiOperation(value = "修改书本信息")
    @PreAuthorize("hasAuthority('book:manage:update')")
    public void updBook(@Validated @RequestBody BookUpdDto bookUpdDto) {
        Book book = CheckEntityUtil.isNullById(bookUpdDto.getId(), bookService, ResultCode.BOOK_ACCOUNT_NOT_EXIST);

        if (bookUpdDto.getTypeId() != null) {
            CheckEntityUtil.isNullById(bookUpdDto.getTypeId(), typeService, ResultCode.BOOK_TYPE_NOT_EXIST);
        }

        bookUpdDto.convertToEntity(book);
        bookService.updateById(book);

    }

    @DeleteMapping
    @Transactional(rollbackFor = {Exception.class})
    @ApiOperation(value = "删除书本信息")
    @PreAuthorize("hasAuthority('book:manage:delete')")
    public void delBook(String ids) {
        List<Integer> bookIds = Arrays.asList(Convert.toIntArray(ids));
        bookService.removeByIds(bookIds);

    }


}
